package com.xdl.ui;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ClassLoaderUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;
import com.intellij.ide.fileTemplates.impl.UrlUtil;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.Notifications;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import com.xdl.model.DataCenter;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.URL;

/*
 *@program:Mark
 *
 *@description:
 *
 *@author:胡博欣
 *
 *@create:2020-05-23-53
 */
public class MarkContent {
    public JTable table1;
    public JButton createButton;
    public JButton clearButton;
    public JButton closeButton;
    public JTextField titleName;
    public JPanel parentPanel;
    public JPanel titlePanel;
    public JPanel buttomPanel;
    public JLabel title;

    private void init(){
        getTemplate();
        table1.setModel(DataCenter.tableModel);
        table1.setEnabled(true);
    }



    MarkContent(@NotNull Project project, @NotNull ToolWindow toolWindow) {
        init();
        NotificationGroup notificationGroup = new NotificationGroup("MarkBook", NotificationDisplayType.BALLOON,false);
        createButton.addActionListener(e -> {
            String text = titleName.getText();
            String fileName=text+".md";
            if(!ObjectUtil.isEmpty(text)){
                VirtualFile virtualFile = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(), project, project.getBaseDir());
                if(!ObjectUtil.isEmpty(virtualFile)){
                    String path=virtualFile.getPath()+"/"+fileName;
                    TemplateEngine engine = TemplateUtil.createEngine();
                    String template1 =   getTemplate();
                    Template template = engine.getTemplate(template1);
                    Dict dict = Dict.create();
                    dict.set("parentTitle",text );
                    dict.set("rowList",DataCenter.LIST );
                    String result = template.render(dict);
                    FileUtil.writeUtf8String(result, path);
                    Notification notification = notificationGroup.createNotification("文档生成成功", MessageType.INFO);
                    Notifications.Bus.notify(notification);
                }
            }else{
                Notification notification = notificationGroup.createNotification("请输入文档标题", MessageType.WARNING);
                Notifications.Bus.notify(notification);
            }
        });
        clearButton.addActionListener(e -> DataCenter.clear());
        closeButton.addActionListener(e -> toolWindow.hide(null));
    }


   private static  String getTemplate(){
       String loadText=null;
       try {
          loadText = UrlUtil.loadText(MarkContent.class.getResource("/templates/markBoot.ftl"));
       } catch (IOException e) {
           return null;
       }
       return loadText;
   }




}
